//
//  LaunchScrollView.swift
//  koolcar
//
//  Created by xiantian on 2023/9/18.
//

import UIKit
import FSPagerView

struct LaunchObject {
    var imgName: String
    var title: String
}

class LaunchScrollView: UIView {
//    var collectionView: UICollectionView!
    var pageControl: FSPageControl!
    var dataArray: [LaunchObject] = []
    var pagerView: FSPagerView!
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.initialValue()
        
        pagerView = FSPagerView()
        pagerView.itemSize = CGSize(width: kScreenWidth, height: 348)
        pagerView.register(LaunchItemCell.self, forCellWithReuseIdentifier: "LaunchItemCell")
        pagerView.dataSource = self
        pagerView.delegate = self
        pagerView.isInfinite = true
        pagerView.bounces = false
        pagerView.automaticSlidingInterval = 3
        self.addSubview(pagerView)
        
        pageControl = FSPageControl.init(frame: CGRect.init(x: 0, y: 0, width: 160, height: 10))
        pageControl.backgroundColor = UIColor.clear
        pageControl.numberOfPages = self.dataArray.count
        self.pageControl.setFillColor(UIColor.init(hex: 0x005C99, alpha: 0.1), for: .normal)
        self.pageControl.setFillColor(Theme.mainColor, for: .selected)
        self.addSubview(pageControl)
        
        self.addSnpLayout()
    }
    
    private func addSnpLayout(){
        pagerView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.top.equalTo(0)
            make.height.greaterThanOrEqualTo(348)
        }
        
        pageControl.snp.makeConstraints { make in
            make.centerX.equalTo(kScreenWidth/2)
            make.top.equalTo(self.pagerView.snp.bottom).offset(10)
            make.height.equalTo(10)
            make.width.equalTo(160)
        }
        
        self.layoutIfNeeded()
    }
    
    func initialValue(){
        self.dataArray = []
        let obj1 = LaunchObject.init(imgName: "launch_scroll_1",
                                     title: MMLocalizedString("launch_online", comment: "线上找车"))
        let obj2 = LaunchObject.init(imgName: "launch_scroll_2",
                                     title: MMLocalizedString("launch_precar", comment: "预约看车"))
        let obj3 = LaunchObject.init(imgName: "launch_scroll_3",
                                     title: MMLocalizedString("launch_transform", comment: "交易过户"))
        let obj4 = LaunchObject.init(imgName: "launch_scroll_4",
                                     title: MMLocalizedString("launch_maintain", comment: "售后维修保养"))
        self.dataArray.append(obj1)
        self.dataArray.append(obj2)
        self.dataArray.append(obj3)
        self.dataArray.append(obj4)
    }
    
    ///刷新
    public func reloadData(){
        self.initialValue()
        self.pagerView.reloadData()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension LaunchScrollView: FSPagerViewDelegate, FSPagerViewDataSource {
    func numberOfItems(in pagerView: FSPagerView) -> Int {
        return self.dataArray.count
    }
    
    func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "LaunchItemCell", at: index) as! LaunchItemCell
        if self.dataArray.count > index {
            let obj = self.dataArray[index]
            cell.imgView.image = UIImage.init(named: obj.imgName)
            cell.titleLab.text = obj.title
        }
        return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        print("Selected item at index \(index)")
    }
    
    func pagerViewWillEndDragging(_ pagerView: FSPagerView, targetIndex: Int) {
        self.pageControl.currentPage = targetIndex
    }
    
    func pagerViewDidEndScrollAnimation(_ pagerView: FSPagerView) {
        self.pageControl.currentPage = pagerView.currentIndex
    }
}

